

// 字符串转对象


// const qs = require('querystring')
// let query1 = 'id=1&name=lisi'
// console.log(qs.parse(query1))

// const qs = require('querystring')
// let query2 = '?id=1&name=lisi'
// const num = query2.indexOf('?')
// const res = query2.slice(num + 1, query2.length)
// console.log(qs.parse(res))


// 对象转化成字符串
// let obj = { id: '1', name: 'lisi' }
// console.log(qs.stringify(obj))

// jQuery添加put delete
// $.ajax({
//     url:"",
//     type:"DELETE",
//     success: function (result) {
        
//     }
// });
// $.ajax({
//     url:"",
//     type:"PUT",
//     success: function (response) {
        
//     }
// })

// 封装Storage
// 字符串转对象
// 定义一个对象 拿到数据存入
/* let obj = {}
    // 验证
    if (!window.localStorage) {
        return alert('浏览器不支持localStorage!')
    }
    // 设置
    obj.set = function (key,value) {
        window.localStorage.setItem(key, JSON.stringify(value))
    }
    // 拿到
    obj.get = function (key,value) {
        var obj = window.localStorage.getItem(key)
        if ( !obj ) return JSON.parse(obj)
    }
    // 删除
    obj.remove = function (key) {
        if (key) window.localStorage.removeItem(key)
    }
    // module.exports = obj */







//字符串转对象

stringify.prototype.query = function(){
    let str = this[0] ==='?' ? this.substr(1) : this
    return str.split('&').reduce( (p,c) =>{
        p[c.split('=')[0]] = c.split('=')[1]
        return p
    },{})
}
console.log('?id=1&name=zhangsan'.query)
console.log('id=1&name=zhangsan'.query)



//jquery

(function a() {
    $.a.put = function (url, data = {}, successFn, dataType = 'text') {
        $.ajax({
            url,
            data,
            type: 'PUT',
            dataType,
            success: successFn
        })
    }
    $.a.delete = function (url, data = {}, successFn, dataType = 'text') {
        $.ajax({
            url,
            data,
            type: 'DELETE',
            dataType,
            success: successFn
        })
    }
})(jQuery)